package com.boarsoft.boar.code.bean.biz.impl;

import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.boarsoft.bean.ReplyInfo;
import com.boarsoft.boar.code.bean.biz.BeanPropBiz;
import com.boarsoft.boar.code.entity.BeanInfo;
import com.boarsoft.boar.code.entity.BeanProp;
import com.boarsoft.common.Util;
import com.boarsoft.common.dao.PagedResult;
import com.boarsoft.hibernate.biz.SimpleBizImpl;

@Component("beanPropBiz")
public class BeanPropBizImpl extends SimpleBizImpl implements BeanPropBiz {

	/**
	 * bean信息查询：/code/bean/info/list.do
	 */
	@Override
	@Transactional(readOnly = true)
	public PagedResult<BeanProp> list(String beanId, int pageNo, int pageSize) {
		StringBuilder sb = new StringBuilder();
		sb.append(" from BeanProp r where 1=1");
		if (Util.strIsNotEmpty(beanId)) {
			sb.append(" and r.beanId like '%").append(beanId).append("%'");
		}
		sb.append(" order by r.id");
		List<BeanProp> lt = this.dao.list(sb.toString(), pageNo, pageSize);
		return new PagedResult<>(lt.size(), lt, pageNo, pageSize);
	}

	/**
	 * bean类型名列表查询：/code/bean/info/find.do
	 */
	@Transactional(readOnly = true)
	@Override
	public ReplyInfo<Object> find(int pageNo, int pageSize) {
		return new ReplyInfo<>(true, new PagedResult<>(dao.getTotal("select count(*) from BeanProp"),
				dao.list(BeanInfo.class, "and 1=1"), pageNo, pageSize));
	}

	/**
	 * 保存bean信息
	 * 
	 * @param beanProp
	 *            bean属性信息
	 * @return 存储数据库的key
	 */
	@Transactional
	@Override
	public String save(BeanProp beanProp) {
		return (String) this.dao.save(beanProp);
	}

	/**
	 * bean属性列表查询
	 */
	@Transactional(readOnly = true)
	@Override
	public List<BeanProp> findBeanPropByBeanId(String beanId, String key) {
		StringBuilder sb = new StringBuilder();
		sb.append(" and beanId = '").append(beanId).append("'");
		if (Util.strIsNotEmpty(key)) {
			sb.append(" and (name like '%").append(key).append("%'").append(" or type like '%").append(key).append("%')");
		}
		String sql = "select r from BeanProp r where 1=1".concat(sb.toString());
		return this.dao.list(sql);
	}

	@Override
	@Transactional
	public void delete(String id) {
		String w = String.format("and id='%s'", id);
		BeanProp beanProp = this.dao.find(BeanProp.class, w);
		if (beanProp != null) {
			this.dao.delete(beanProp);
		}
	}

	@Override
	@Transactional(readOnly = true)
	public String findPropType(String name, String beanClazz) {
		String w = String.format("and vName='%s' and vBeanClazz='%s'",name,beanClazz);
		BeanProp beanProp = this.dao.find(BeanProp.class, w);
		return beanProp.getValue();
	}

	@Override
	@Transactional(readOnly = true)
	public Map<String, String> findPropTypeTree(Map<String, String> map,String beanClazz) {
		String w = String.format("and vBeanClazz='%s'",beanClazz);
		List<BeanProp> list = this.dao.list(BeanProp.class, w);
		if(list==null){
			return map;
		}
		for (BeanProp beanProp:list){
			map.put(beanProp.getBeanClazz().concat("_").concat(beanProp.getName()),beanProp.getValue());
			findPropTypeTree(map,beanProp.getValue());
		}
		return map;
	}
}
